import dwv from "dwv"

const overlayMaps = [
  {
    tags: ["x00100020"],
    pos: "tl"
  },
  {
    tags: ["x00100010", "x00100040"],
    pos: "tl"
  },
  {
    tags: ["x00100030"],
    pos: "tl"
  },
  {
    tags: ["x00200011", "x00200012", "x00200013"],
    pos: "bl"
  },
  {
    tags: ["x00185100"],
    pos: "bl"
  },
  {
    tags: ["x00201041"],
    pos: "bl"
  },
  {
    tags: ["x00180050"],
    pos: "bl"
  },
  {
    tags: ["x00280010", "x00280011"],
    pos: "bl"
  },
  {
    tags: ["x00181100"],
    pos: "bl"
  },
  {
    tags: ["x00080080"],
    pos: "tr"
  },
  {
    tags: ["x00080020", "x00080030"],
    pos: "tr"
  },
  {
    tags: ["x00080060"],
    pos: "tr"
  },
  {
    key: "window-center",
    value: "",
    pos: "br",
    format: "窗位:"
  },
  {
    key: "window-width",
    value: "",
    pos: "br",
    format: "窗宽:"
  },
  {
    key: "position",
    value: "",
    pos: "br",
    format: "位置:"
  },
  {
    key: "value",
    value: "",
    pos: "br",
    format: "值:"
  },
  {
    key: "frame",
    value: "",
    pos: "br",
    format: "频谱:"
  }
]

const createOverlayData = dicomElements => {
  let overlays = {}
  let modality = dicomElements.getFromKey("x00080060")
  if (!modality) {
    return overlays
  }
  let omaps = overlayMaps

  if (!omaps) {
    return overlays
  }

  for (let n = 0; omaps[n]; n++) {
    let value = omaps[n].value
    let tags = omaps[n].tags
    let format = omaps[n].format
    let pos = omaps[n].pos

    if (typeof tags !== "undefined" && tags.length !== 0) {
      let values = []
      for (let i = 0; i < tags.length; ++i) {
        values.push(dicomElements.getElementValueAsStringFromKey(tags[i]))
      }
      // 格式化
      if (typeof format === "undefined" || format === null) {
        format = dwv.utils.createDefaultReplaceFormat(values)
      }
      value = dwv.utils.replaceFlags2(format, values)
    }

    if (!overlays[pos]) {
      overlays[pos] = []
    }

    overlays[pos].push({
      value: value.trim(),
      format: omaps[n].format,
      key: omaps[n].key
    })
  }

  let valuePO = dicomElements.getFromKey("x00200020")
  if (
    typeof valuePO !== "undefined" &&
    valuePO !== null &&
    valuePO.length === 2
  ) {
    let po0 = dwv.dicom.cleanString(valuePO[0])
    let po1 = dwv.dicom.cleanString(valuePO[1])
    overlays.cr = [{ value: po0 }]
    overlays.cl = [{ value: dwv.dicom.getReverseOrientation(po0) }]
    overlays.bc = [{ value: po1 }]
    overlays.tc = [{ value: dwv.dicom.getReverseOrientation(po1) }]

    console.log("getReverse", "po0", po0, dwv.dicom.getReverseOrientation(po0))
  }

  console.log("overlays", overlays)

  return overlays
}

const handleInfos = event => {
  let data = event.data
  let infos = []
  if (typeof data.x00020010 !== "undefined") {
    infos = createOverlayData(new dwv.dicom.DicomElementsWrapper(data))
  }

  return infos
}

const update = (e, data) => {
  data.forEach(l => {
    if (e.type === "position-change") {
      if (l.key === "position") {
        l.value = `${e.i},${e.j},${e.k}`
      }
      if (l.key === "value") {
        l.value = `${e.value}`
      }
    }

    if (e.type === "wl-width-change") {
      if (l.key === "window-width") {
        l.value = e.ww
      }
    }

    if (e.type === "wl-center-change") {
      if (l.key === "window-center") {
        l.value = e.wc
      }
    }
  })


  return data
}

export default {
  createOverlayData,
  handleInfos,
  update
}
